﻿Math.guid = function () {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    }).toUpperCase();
};
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
}
//网站搜索
$.BENLAI.searchModel = {};
var img = new Image();
$.BENLAI.searchModel.random = Math.random();
$(document).ready(function () {
    //1.2 数据采集
    $.BENLAI.searchModel.KeyWords = decodeURI(escape(getQueryString("keyword")));
    $.BENLAI.searchModel.WebSite = getCookie("WebSiteSysNo");
    $.BENLAI.searchModel.ClientSource = 0;//网站 0    
    $.BENLAI.searchModel.ProductCount = $('[data-type="ProductTotalNum"]').html();    
    var searchSource = $.BENLAI.replace("KeyWords={KeyWords}&WebSite={WebSite}&ClientSource={ClientSource}&ProductCount={ProductCount}&random={random}", $.BENLAI.searchModel);   
    if (getQueryString("sType") == "Search") {
        img.src = "http://searchtracking.benlai.com/search.ico?" + searchSource;       
    } else {
        return;
    }
  
  
    //2.2 数据采集
    $.BENLAI.searchModel.vk = getCookie('userGuid');
    $.BENLAI.searchModel.usk = Math.guid();
    $.BENLAI.searchModel.rt;
    $.BENLAI.searchModel.cp;
    $.BENLAI.searchModel.sku;
    $.BENLAI.searchModel.sk = decodeURI(escape(getQueryString("keyword")));
    $.BENLAI.searchModel.p;
    $.BENLAI.searchModel.tp;
    $.BENLAI.searchModel.site = getCookie("WebSiteSysNo");
    $.BENLAI.searchModel.src = 0;
    $.BENLAI.searchModel.v = "";
    $.BENLAI.searchModel.s;
    $.BENLAI.searchModel.sort;
    $(('[data-type="ProductList"]')).delegate("dl", "click", function (e) {
        var _pageEndTime = new Date();
        rt = _pageEndTime.getTime() - _pageStartTime.getTime();
        $.BENLAI.searchModel.rt = rt;
        var index = $(this).index();
        sku = $('[data-type="AddCartBtn"]').eq(index).attr("product");
        $.BENLAI.searchModel.sku = sku.replace(/(^\s*)|(\s*$)/g, "");
        $.BENLAI.searchModel.p = index + 1;
        $.BENLAI.searchModel.cp = BENLAI.Model.ListTemp.PageNum;
        $.BENLAI.searchModel.tp = BENLAI.Model.ListTemp.TotalPageNum;
        $.BENLAI.searchModel.s = 'search';
        $.BENLAI.searchModel.sort = BENLAI.Model.ListTemp.Sort;
        var clickSearch = $.BENLAI.replace("vk={vk}&usk={usk}&rt={rt}&cp={cp}&sku={sku}&sk={sk}&p={p}&tp={tp}&site={site}&src={src}&v={v}&s={s}&sort={sort}&random={random}", $.BENLAI.searchModel);
        img.src = "http://searchtracking.benlai.com/search.ico?" + clickSearch;
    });
});
$(function () {
    if ($('#ListBox').length > 0) {
        $('#ListBox').CreateController({
            events: {
                '[data-type="c2sysnodt"] click': 'ShowC3',
                '[data-type="Guide"] click': 'Guide',
                '[data-type="AddCartBtn"] click': 'AddCart',
                '[data-type="ListPlus"] click': 'Plus',
                '[data-type="ListMinus"] click': 'Minus',
                '[data-type="ListSort"] click': 'Sort',
                '[data-type="gotopage"] click': 'GoToPage',
                '[data-type="pageprevious"] click': 'Previous',
                '[data-type="pagenext"] click': 'Next',
                '[data-type="pagenum"] click': 'Page',
                '[data-type="JdPagePrevious"] click': 'JdPagePrevious',
                '[data-type="JdPageNext"] click': 'JdPageNext'
            },
            bind: function () {
                $.BENLAI.ajax({
                    type: 'POST',
                    cache: false,
                    url: '/NewCategory/GetLuceneProduct',
                    data: {
                        c1: BENLAI.Model.ListTemp.C1,
                        c2: BENLAI.Model.ListTemp.C2,
                        c3: BENLAI.Model.ListTemp.C3,
                        sort: BENLAI.Model.ListTemp.Sort,
                        filter: BENLAI.Model.ListTemp.Filter,
                        Page: BENLAI.Model.ListTemp.PageNum
                    },
                    success: function (result) {
                        if (AjaxValidate(result)) {
                            BENLAI.Model.ListModel = result;
                            if (result.TotalNum > 0) {
                                BENLAI.Model.ListTemp.PageNum = result.SelectNum;
                                BENLAI.Model.ListTemp.TotalPageNum = result.TotalNum;
                                $('[data-type="ProductList"]').show();
                                $('[data-type="PageFen"]').show();
                                $.BENLAI.tmpl({ dom: $('[data-type="ProductList"]'), v: BENLAI.Model.ListTemp.ListProduct, datas: BENLAI.Model.ListModel });
                                $.BENLAI.tmpl({ dom: $('[data-type="PageFen"]'), v: BENLAI.Model.ListTemp.PageTemp, datas: BENLAI.Model.ListModel });
                                $('[data-type="ProductTotalNum"]').html(result.TotalProductNum);
                                $('[data-type="PageSelectNum"]').html(result.SelectNum + "/" + result.TotalNum);
                                $('#divNoResult').hide();
                            } else {
                                BENLAI.Model.ListTemp.PageNum = 1;
                                $('[data-type="ProductList"]').hide();
                                $('[data-type="PageFen"]').hide();
                                $('[data-type="ProductTotalNum"]').html("0");
                                $('#divNoResult').show();
                            }
                            lazyLoad.LoadImage();
                            smoothScroll($(".box970").offset().top);
                        } else {
                            alert("加载错误，刷新后重新加载！");
                        }
                    }
                });
            },
            ShowC3: function (t, event) {
                $('[data-type="c2sysnodd"]').hide();
                if ($(t).attr("class") != "on_b") {
                    $('[data-type="c2sysnodt"]').removeClass("on_b");
                    var num = $(t).attr("num");
                    $(t).addClass("on_b");
                    $('[data-type="c2sysnodd"][num="' + num + '"]').show();
                } else {
                    $('[data-type="c2sysnodt"]').removeClass("on_b");
                    var num = $(t).attr("num");
                    $('[data-type="c2sysnodd"][num="' + num + '"]').hide();
                }
            },
            Guide: function (t, event) {
                var gno = $(t).attr("gno");
                $('[data-type="Guide"][gno="' + gno + '"]').removeClass("on");
                $(t).addClass("on");
                var guide = $('.on[data-type="Guide"]');
                BENLAI.Model.ListTemp.Filter = "";
                $.each(guide, function (k, v) {
                    if ($(v).attr("guideid") != "0") {
                        if (!BENLAI.Model.ListTemp.Filter) {
                            BENLAI.Model.ListTemp.Filter += $(v).attr("guideid");
                        } else {
                            BENLAI.Model.ListTemp.Filter += ",";
                            BENLAI.Model.ListTemp.Filter += $(v).attr("guideid");
                        }
                    }
                });
                this.bind();
            },
            Plus: function (t, event) {
                var input = $(t).parent().find('[data-type="ListInput"]');
                if (input.val() >= 999) {
                    input.val(999);
                    return false;
                }
                input.val(Number(input.val()) + 1);
            },
            Minus: function (t, event) {
                var input = $(t).parent().find('[data-type="ListInput"]');
                if (input.val() <= 1) {
                    input.val(1);
                    return false;
                }
                input.val(Number(input.val()) - 1);
            },
            Sort: function (t, event) {
                BENLAI.Model.ListTemp.PageNum = 1;
                var num = $(t).attr("num");
                var num2 = $(t).attr("num2");
                var div = $(t).parent().find("div");
                var NowNum = num2;
                if (div) {
                    var divclass = div.attr("class");
                    if (divclass == "up") {
                        NowNum = num;
                    }
                }
                var ul = $(t).parent().parent();
                ul.find("a").removeClass("on");
                ul.find("div").removeAttr("class");
                if (div.length > 0) {
                    $(t).addClass("on");
                    if (NowNum == num2) {
                        div.addClass("up");
                    } else if (NowNum == num) {
                        div.addClass("down");
                    }
                }
                BENLAI.Model.ListTemp.Sort = NowNum;
                this.bind();
            },
            GoToPage: function (t, event) {
                BENLAI.Model.ListTemp.PageNum = Number($('[data-type="pagenumInput"]').val());
                this.bind();
            },
            Previous: function (t, event) {
                if ($(t).attr("class") == "link_none")
                    return false;
                var num = Number($('.on[data-type="pagenum"]').html());
                BENLAI.Model.ListTemp.PageNum = num - 1;
                this.bind();
            },
            Next: function (t, event) {
                if ($(t).attr("class") == "link_none")
                    return false;
                var num = Number($('.on[data-type="pagenum"]').html());
                BENLAI.Model.ListTemp.PageNum = num + 1;
                this.bind();
            },
            JdPagePrevious: function (t, event) {
                if (BENLAI.Model.ListTemp.PageNum <= 1)
                    return false;
                var num = Number($('.on[data-type="pagenum"]').html());
                BENLAI.Model.ListTemp.PageNum = num - 1;
                this.bind();
            },
            JdPageNext: function (t, event) {
                if (BENLAI.Model.ListTemp.PageNum >= BENLAI.Model.ListTemp.TotalPageNum)
                    return false;
                var num = Number($('.on[data-type="pagenum"]').html());
                BENLAI.Model.ListTemp.PageNum = num + 1;
                this.bind();
            },
            Page: function (t, event) {
                BENLAI.Model.ListTemp.PageNum = Number($(t).html());
                this.bind();
            },
            AddCart: function (t, event) {
                if ($(t).attr("status") == "0") {
                    AddProductToCart($(t).attr("product"), $(t).parent().parent().find('[data-type="ListInput"]').val())
                } else if ($(t).attr("status") == "1") {
                    NotifyAdding($(t).attr("product"), 1);
                }
            },
            init: function () {
                BENLAI.Model.ListTemp.ListProduct = $("#ListProduct").val();
                BENLAI.Model.ListTemp.PageTemp = $("#PageTemp").val();
                BENLAI.Model.ListTemp.PageNum = 1;
                BENLAI.Model.ListTemp.TotalPageNum = Number($('[data-type="pagenum"]').last().html());
                BENLAI.Model.ListTemp.Sort = 0;
                BENLAI.Model.ListTemp.Filter = "";
                BENLAI.Model.ListTemp.C1 = $('[data-type="c1sysno"]').attr("num");
                BENLAI.Model.ListTemp.C2 = $('.on_b[data-type="c2sysnodt"]').attr("num") || "-999999";
                BENLAI.Model.ListTemp.C3 = $('.on_s[data-type="c3sysno"]').attr("num") || "-999999";
            }
        });
    } else {
        $('#SearchBox').CreateController({
            events: {
                '[data-type="c2sysnodt"] click': 'ShowC3',
                '[data-type="c3sysno"] click': 'SearchC3',
                '[data-type="AddCartBtn"] click': 'AddCart',
                '[data-type="ListPlus"] click': 'Plus',
                '[data-type="ListMinus"] click': 'Minus',
                '[data-type="ListSort"] click': 'Sort',
                '[data-type="gotopage"] click': 'GoToPage',
                '[data-type="pageprevious"] click': 'Previous',
                '[data-type="pagenext"] click': 'Next',
                '[data-type="pagenum"] click': 'Page',
                '[data-type="JdPagePrevious"] click': 'JdPagePrevious',
                '[data-type="JdPageNext"] click': 'JdPageNext'
            },
            bind: function () {
                $.BENLAI.ajax({
                    type: 'POST',
                    cache: false,
                    url: '/NewCategory/GetSearchList',
                    data: {
                        c1: BENLAI.Model.ListTemp.C1,
                        c3: BENLAI.Model.ListTemp.C3,
                        sort: BENLAI.Model.ListTemp.Sort,
                        Page: BENLAI.Model.ListTemp.PageNum,
                        keyWord: BENLAI.Model.ListTemp.keyWord,
                        oldKeys: BENLAI.Model.ListTemp.keyWord
                    },
                    success: function (result) {
                        if (AjaxValidate(result)) {
                            BENLAI.Model.ListModel = result;
                            if (result.TotalNum > 0) {
                                BENLAI.Model.ListTemp.PageNum = result.SelectNum;
                                BENLAI.Model.ListTemp.TotalPageNum = result.TotalNum;
                                $('[data-type="ProductList"]').show();
                                $('[data-type="PageFen"]').show();
                                $.BENLAI.tmpl({ dom: $('[data-type="ProductList"]'), v: BENLAI.Model.ListTemp.ListProduct, datas: BENLAI.Model.ListModel.ProductList });
                                $.BENLAI.tmpl({ dom: $('[data-type="PageFen"]'), v: BENLAI.Model.ListTemp.PageTemp, datas: BENLAI.Model.ListModel });
                                $('[data-type="ProductTotalNum"]').html(result.TotalProductNum);
                                $('[data-type="PageSelectNum"]').html(result.SelectNum + "/" + result.TotalNum);
                                $('#divNoResult').hide();

                                //上报时机：用户翻页时
                                $.BENLAI.searchModel.vk = getCookie('userGuid');
                                $.BENLAI.searchModel.usk = Math.guid();
                                $.BENLAI.searchModel.sk = $('[benlai-id="searchkeyword"]').val();
                                $.BENLAI.searchModel.site = getCookie("WebSiteSysNo");
                                $.BENLAI.searchModel.src = 0;
                                var _pageEndTime = new Date();
                                $.BENLAI.searchModel.rt = _pageEndTime.getTime() - _pageStartTime.getTime();
                                $.BENLAI.searchModel.cp = BENLAI.Model.ListTemp.PageNum;
                                $.BENLAI.searchModel.tp = BENLAI.Model.ListTemp.TotalPageNum;
                                $.BENLAI.searchModel.v = "";
                                $.BENLAI.searchModel.s = 'flip';
                                $.BENLAI.searchModel.sort = BENLAI.Model.ListTemp.Sort;
                                $.BENLAI.searchModel.random = Math.random();
                                var flipSearch = $.BENLAI.replace("vk={vk}&usk={usk}&rt={rt}&cp={cp}&sk={sk}&tp={tp}&site={site}&src={src}&v={v}&s={s}&sort={sort}&random={random}", $.BENLAI.searchModel);
                                img.src = "http://searchtracking.benlai.com/search.ico?" + flipSearch;
                            } else {
                                BENLAI.Model.ListTemp.PageNum = 1;
                                $('[data-type="ProductList"]').hide();
                                $('[data-type="PageFen"]').hide();
                                $('[data-type="ProductTotalNum"]').html("0");
                                $('#divNoResult').show();
                            }
                            lazyLoad.LoadImage();
                            smoothScroll($(".box970").offset().top);
                        } else {
                            alert("加载错误，刷新后重新加载！");
                        }
                    }
                });
            },
            ShowC3: function (t, event) {
                $('[data-type="c2sysnodd"]').hide();
                if ($(t).attr("class") != "on_b") {
                    $('[data-type="c2sysnodt"]').removeClass("on_b");
                    var num = $(t).attr("num");
                    $(t).addClass("on_b");
                    $('[data-type="c2sysnodd"][num="' + num + '"]').show();
                } else {
                    $('[data-type="c2sysnodt"]').removeClass("on_b");
                    var num = $(t).attr("num");
                    $('[data-type="c2sysnodd"][num="' + num + '"]').hide();
                }
            },
            SearchC3: function (t, event) {
                if ($(t).attr("class") == "on_s")
                    return false;
                $('[data-type="c3sysno"]').removeClass("on_s");
                $(t).addClass("on_s");
                BENLAI.Model.ListTemp.C3 = Number($(t).attr("no"));
                BENLAI.Model.ListTemp.PageNum = 1;
                this.bind();
            },
            Plus: function (t, event) {
                var input = $(t).parent().find('[data-type="ListInput"]');
                if (input.val() >= 999) {
                    input.val(999);
                    return false;
                }
                input.val(Number(input.val()) + 1);
            },
            Minus: function (t, event) {
                var input = $(t).parent().find('[data-type="ListInput"]');
                if (input.val() <= 1) {
                    input.val(1);
                    return false;
                }
                input.val(Number(input.val()) - 1);
            },
            Sort: function (t, event) {
                BENLAI.Model.ListTemp.PageNum = 1;
                var num = $(t).attr("num");
                var num2 = $(t).attr("num2");
                var div = $(t).parent().find("div");
                var NowNum = num2;
                if (div) {
                    var divclass = div.attr("class");
                    if (divclass == "up") {
                        NowNum = num;
                    }
                }
                var ul = $(t).parent().parent();
                ul.find("a").removeClass("on");
                ul.find("div").removeAttr("class");
                if (div.length > 0) {
                    $(t).addClass("on");
                    if (NowNum == num2) {
                        div.addClass("up");
                    } else if (NowNum == num) {
                        div.addClass("down");
                    }
                }
                BENLAI.Model.ListTemp.Sort = NowNum;
                this.bind();
            },
            GoToPage: function (t, event) {
                BENLAI.Model.ListTemp.PageNum = Number($('[data-type="pagenumInput"]').val());
                this.bind();
            },
            Previous: function (t, event) {
                if ($(t).attr("class") == "link_none")
                    return false;
                var num = Number($('.on[data-type="pagenum"]').html());
                BENLAI.Model.ListTemp.PageNum = num - 1;
                this.bind();
            },
            Next: function (t, event) {
                if ($(t).attr("class") == "link_none")
                    return false;
                var num = Number($('.on[data-type="pagenum"]').html());
                BENLAI.Model.ListTemp.PageNum = num + 1;
                this.bind();
            },
            JdPagePrevious: function (t, event) {
                if (BENLAI.Model.ListTemp.PageNum <= 1)
                    return false;
                var num = Number($('.on[data-type="pagenum"]').html());
                BENLAI.Model.ListTemp.PageNum = num - 1;
                this.bind();
            },
            JdPageNext: function (t, event) {
                if (BENLAI.Model.ListTemp.PageNum >= BENLAI.Model.ListTemp.TotalPageNum)
                    return false;
                var num = Number($('.on[data-type="pagenum"]').html());
                BENLAI.Model.ListTemp.PageNum = num + 1;
                this.bind();
            },
            Page: function (t, event) {
                BENLAI.Model.ListTemp.PageNum = Number($(t).html());
                this.bind();
            },
            AddCart: function (t, event) {
                if ($(t).attr("status") == "0") {
                    AddProductToCart($(t).attr("product"), $(t).parent().parent().find('[data-type="ListInput"]').val())
                } else if ($(t).attr("status") == "1") {
                    NotifyAdding($(t).attr("product"), 1);
                }
            },
            init: function () {
                BENLAI.Model.ListTemp.ListProduct = $("#hotproudctTemplate").val();
                BENLAI.Model.ListTemp.PageTemp = $("#PageTemp").val();
                BENLAI.Model.ListTemp.PageNum = 1;
                BENLAI.Model.ListTemp.TotalPageNum = Number($('[data-type="pagenum"]').last().html());
                BENLAI.Model.ListTemp.Sort = 0;
                BENLAI.Model.ListTemp.keyWord = $('[data-type="c1sysno"]').attr("keyname");
                BENLAI.Model.ListTemp.C1 = 0;
                BENLAI.Model.ListTemp.C3 = 0;
                HotProduct["title"] = "热销榜";
                $.BENLAI.tmpl({ dom: $('[data-type="hotproduct"]'), v: $('#lefthot').val(), datas: HotProduct });
                var SearchKey = $('[data-type="sitemap"] a').last().html();
                if (SearchKey == "" || SearchKey == "首页") {
                    if (BENLAI.SearchKeyWords) {
                        SearchKey = BENLAI.SearchKeyWords;
                    } else {
                        SearchKey = "";
                    }
                }
                $('[benlai-id="searchkeyword"]').val(SearchKey);
            }
        });
    }
});
BENLAI.Model.ListTemp = {};